package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.camera.Camera;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.upLoad.Base64ToString;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.FBgvAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class FBPCActivity extends BaseActivity implements NetCallback{
    private FanHuiView title;
    private LinearLayout ll_pc_cp,ll_pc_zp;
    private EditText start,zhongdian,time,lianxiren,lianxidianhua,car,miaoshu;
    private TextView fabu;
    private GridView gv;
    private FBgvAdapter adapter;
    private List<Bitmap> list;
    private String biaoti;
    private ImageView iv_xiangji;
    @Override
    protected void init() {
        setContentView(R.layout.linli_pinchefabu);
        title = find(R.id.title);
        ll_pc_cp = find(R.id.ll_pc_cp);
        start = find(R.id.start);
        zhongdian = find(R.id.zhongdian);
        time = find(R.id.time);
        lianxiren = find(R.id.lianxiren);
        lianxidianhua = find(R.id.lianxidianhua);
        car = find(R.id.car);
        miaoshu = find(R.id.miaoshu);
        ll_pc_zp = find(R.id.ll_pc_zp);
        fabu = find(R.id.fabu);
        gv = find(R.id.gv);
        iv_xiangji = find(R.id.iv_xiangji);
        list = new ArrayList<>();
        Intent intent = getIntent();
        biaoti = intent.getStringExtra("title");
        title.getTv_title().setText(biaoti);
        if (biaoti.equals("找拼车")) {
            ll_pc_zp.setVisibility(View.GONE);
            ll_pc_cp.setVisibility(View.GONE);
        }
        adapter = new FBgvAdapter(list, getApplicationContext());
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (list.size() == position) {
                    Camera.setOnClickListener(iv_xiangji, FBPCActivity.this);
                }
            }
        });
        fabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (biaoti.equals("找乘客")){
                    getNet(1);
                }else {
                    getNet(2);
                }
            }
        });
    }

    private void getNet(int i) {
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String uerrId = sp.getString("userId","");
        Net net=new Net();
        net.url= API.CARPOOLINGINFO_URL;
        net.nameSpace=API.CARPOOLINGINFO_NAMESPACE;
        net.methodName=API.CARPOOLINGINFOSAVE;//carPoolingInfoSave
        Map<String,Object>map=new HashMap();
        map.put("id","");
        map.put("start",start.getText().toString().trim());
        map.put("end",zhongdian.getText().toString().trim());
        map.put("carInfo",car.getText().toString().trim());
        map.put("startTime",time.getText().toString().trim());
        map.put("content",miaoshu.getText().toString().trim());
        map.put("contact",lianxiren.getText().toString().trim());
        map.put("contactPhone",lianxidianhua.getText().toString().trim());
        map.put("tempFileIds",getImageStr());
        map.put("comIds","");
        map.put("userId",uerrId);
        switch (i){
            case 1:
                map.put("infoType","zck");
                break;
            case 2:
                map.put("infoType","zcz");
                break;
        }
        net.parms=map;
        getServer(net,this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Camera.dialog.dismiss();
        switch (requestCode) {
            case Camera.PAI:
                if (resultCode != RESULT_CANCELED) {
                    try {
                        FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory() + "/image.jpg");
                        Bitmap bitmap = BitmapFactory.decodeStream(in);
                        list.add(bitmap);
                        adapter.notifyDataSetChanged();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case Camera.XUAN:
                if (resultCode != RESULT_CANCELED) {
                    ContentResolver resolver = getContentResolver();
                    InputStream in = null;
                    try {
                        in = resolver.openInputStream(data.getData());
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    Bitmap bitmap = BitmapFactory.decodeStream(in);
                    list.add(bitmap);
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }


    @Override
    public void onResponse(boolean flag, int code, String res) {
        try {
            JSONObject object=new JSONObject(res);
            Toast.makeText(FBPCActivity.this,object.getString("msg"),Toast.LENGTH_SHORT).show();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    private String getImageStr(){
        String strImg = "";
        for(int i = 0;i<list.size();i++){
            strImg+= Base64ToString.toString(list.get(i));
            //strImg+=";";
        }
        return  strImg;
    }
}
